package com.k52group.vn.micromarket;

import java.util.ArrayList;
import java.util.List;

import android.app.ProgressDialog;
import android.content.Context;
import android.location.Location;
import android.os.AsyncTask;
import android.widget.ArrayAdapter;
import android.widget.Spinner;
import android.widget.Toast;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.k52group.vn.micromarket.classes.Profile;
import com.k52group.vn.micromarket.classes.Service;
import com.k52group.vn.micromarket.utils.Constant;
import com.k52group.vn.micromarket.utils.HttpRequest;
import com.k52group.vn.micromarket.utils.Utils;

public class LoadServiceTask extends AsyncTask<String, String, String> {
	Context context;
	Spinner sp;
	ProgressDialog dialog;
	boolean isFalse;
	boolean isSearchClick = false;
	List<Service> services = new ArrayList<Service>();

	public boolean isSearchClick() {
		return isSearchClick;
	}

	public void setSearchClick(boolean isSearchClick) {
		this.isSearchClick = isSearchClick;
	}

	public LoadServiceTask(Context context, Spinner sp) {
		// TODO Auto-generated constructor stub
		this.context = context;
		this.sp = sp;
		this.isSearchClick = false;
	}

	public LoadServiceTask(Context context, Spinner sp, boolean issearchclick) {
		// TODO Auto-generated constructor stub
		this.context = context;
		this.sp = sp;
		this.isSearchClick = issearchclick;
	}

	@Override
	protected void onPreExecute() {
		// TODO Auto-generated method stub
		super.onPreExecute();
		dialog = new ProgressDialog(context);
		dialog.setMessage("Loading ...");
		dialog.show();
	}

	@Override
	protected String doInBackground(String... params) {
		// TODO Auto-generated method stub
		try {
			HttpRequest request = HttpRequest.get(Constant.URL_SERVICE);
			if (isSearchClick()) {
				request.form("regId", Utils.getData(Constant.GCMID, context));
				request.form("dns", Utils.getDeviceID(context));
				Location location = Utils.getLocation(context);
				if (location != null) {
					request.form("lat", location.getLatitude());
					request.form("long", location.getLongitude());
				}
			}

			String respone = request.body();
			System.out.println("respone " + respone);
			if (!isSearchClick)
				services = new Gson().fromJson(respone, new TypeToken<ArrayList<Service>>() {
				}.getType());
			else {
				String data[] = respone.split("tranmademicro1");
				for (String string : data) {
					System.out.println("respone " + string);
				}
				System.out.println("respone 0" + data[0]);
				System.out.println("respone 1" + data[1]);
				services = new Gson().fromJson(data[0], new TypeToken<ArrayList<Service>>() {
				}.getType());
				Profile profile = new Gson().fromJson(data[1], Profile.class);
				if (profile != null) {
					Utils.saveData(Constant.USER, new Gson().toJson(profile), context);
				}
			}
			if (services == null) {
				services = new ArrayList<Service>();
				services.add(new Service(0, "Empty"));
			}

		} catch (Exception e) {
			e.printStackTrace();
			isFalse = true;
		}
		return null;
	}

	@Override
	protected void onPostExecute(String result) {
		// TODO Auto-generated method stub
		super.onPostExecute(result);
		dialog.dismiss();
		if (isFalse) {
			Toast.makeText(context, "Can't get service data !", Toast.LENGTH_SHORT).show();
		} else {
			try {
				ArrayAdapter<Service> spinnerAdapter = new ArrayAdapter<Service>(context,
						android.R.layout.simple_spinner_item, services);
				spinnerAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
				sp.setAdapter(spinnerAdapter);
			} catch (Exception e) {
				e.printStackTrace();
				Toast.makeText(context, "Can't get service data !", Toast.LENGTH_SHORT).show();
			}
		}
	}
}
